<?php

/*
 * Edit by xinli
 * Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
 */

namespace api\mall\controller;

use api\mall\model\NotificationModel;

use cmf\controller\RestBaseController;

/**
 * @OA\Tag(
 *     name="mall-op-setting",
 *     description="运营管理"
 * )
 */
class OpSettingController extends RestBaseController
{
    protected function initialize()
    {
        parent::initialize();
        if (empty($this->user)) {
            $this->error('login');
        }
        online_record($this->userId);
    }

    /**
     * 获取客服信息
     * @OA\Get(
     *     tags={"mall-op-setting"},
     *     path="/mall/opSetting/getService",
     *     @OA\Response(response=200, ref="#/components/responses/200")
     * )
     */
    public function getService()
    {
        $mall = cmf_get_option('mall');
        $ret = [];
        $ret['connect_img'] = cmf_get_image_url($mall['connect_img']);
        $ret['connect_mobile'] = $mall['connect_mobile'];
        $ret['connect_address'] = $mall['connect_address'];
        $this->success('ok', $ret);
    }

    /**
     * 获取通知信息
     * @OA\Get(
     *     tags={"mall-op-setting"},
     *     path="/mall/opSetting/getNotification",
     *     @OA\Response(response=200, ref="#/components/responses/200")
     * )
     */
    public function getNotification()
    {
        $notificationModel = new NotificationModel();
        $notifications = $notificationModel->order('notification_type desc')->order('sort_order desc')->select();
        foreach ($notifications as &$notification) {
            // 判断detail长度为0
            if (strlen($notification['detail']) == 0) {
                $notification['detail_url'] = '';
            }
            else {
                $notification['detail_url'] = cmf_url('portal/Page/notification', array('id' => $notification['id']));
            }
            unset($notification['detail']);
        }
        $this->success('ok', $notifications);
    }
}
